package com.lhh.adminlte.controller;

import com.lhh.adminlte.entity.Member;
import com.lhh.adminlte.model.JsonResponse;
import com.lhh.adminlte.model.PageResponse;
import com.lhh.adminlte.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/31 09:44
 * @Description:
 */
@Controller
@RequestMapping(value = "member", produces = "application/json; charset=UTF-8")
public class MemberController extends BaseController {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String PATH = "/member";

    @Autowired
    private IMemberService memberService;

    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, String>> options = new ArrayList<Map<String, String>>();
        Map<String, String> option1 = new HashMap<>();
        option1.put("name", "正常");
        option1.put("value", "1");
        options.add(option1);
        Map<String, String> option2 = new HashMap<>();
        option2.put("name", "禁用");
        option2.put("value", "2");
        options.add(option2);
        modelAndView.addObject("options", options);
        modelAndView.setViewName(PATH + "/index");
        return modelAndView;
    }

    @RequestMapping("list")
    @ResponseBody
    public String list(@RequestParam("phone") String phone, @RequestParam("status") Integer status,
                       @RequestParam("bonusMin") Integer bonusMin, @RequestParam("bonusMax") Integer bonusMax,
                       @RequestParam(value = "createDateStart", required = false) String createDateStartStr, @RequestParam(value = "createDateEnd", required = false) String createDateEndStr,
                       @RequestParam("displayStart") Integer displayStart, @RequestParam("displayLength") Integer displayLength) throws ParseException {

        Date createDateStart = null;
        Date createDateEnd = null;
        if (createDateStartStr != null) {
            createDateStart = sdf.parse(createDateStartStr);
        }
        if (createDateEndStr != null) {
            createDateEnd = sdf.parse(createDateEndStr);
        }
        List<Member> members = memberService.selectMembers(phone, status, bonusMin, bonusMax, createDateStart, createDateEnd, displayStart, displayLength);
        long count = memberService.countMembers(phone, status, bonusMin, bonusMax, createDateStart, createDateEnd);
        PageResponse pageResponse = new PageResponse(count, members);
        return new JsonResponse(200, "success", pageResponse).toString();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam("id") int id, @RequestParam("status") int status) {
        boolean res = memberService.updateMember(id, status);
        if (res) {
            return new JsonResponse(200, "success", null).toString();
        } else {
            return new JsonResponse(202, "fail", null).toString();
        }
    }
}
